package br.schindler.games.android.gl.base;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import br.schindler.games.android.gl.wallpaper.GLWallpaperService;

/**
 * Makes the scene and contros the FPS
 * @author schindler
 */
public class GLWorld implements GLWallpaperService.Renderer  {
	
	/**
	 * All meshes 
	 */
	private List<GLDrawable> meshes = new ArrayList<GLDrawable>();

	
	/**
	 * Add a new mesh
	 * @param mesh
	 */
	public void addMesh(GLDrawable mesh){
		this.meshes.add(mesh);
	}

	/*
	 * (non-Javadoc)
	 * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
	 */
	@Override
	public void onDrawFrame(GL10 gl) {		
	    
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);		
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glPushMatrix();
        gl.glLoadIdentity();

        for(GLDrawable obj : meshes)
        	obj.drawFrame(gl);

		gl.glPopMatrix();	
	}

	/*
	 * (non-Javadoc)
	 * @see android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10, int, int)
	 */
	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		
	   for(GLDrawable obj : meshes)
		  obj.init(gl, width, height);
		
		
		if(height == 0) 						//Prevent A Divide By Zero By
			height = 1; 						//Making Height Equal One

		gl.glViewport(0, 0, width, height); 	//Reset The Current Viewport
		gl.glEnable(GL10.GL_NORMALIZE);
		 
		gl.glMatrixMode(GL10.GL_PROJECTION); 	//Select The Projection Matrix
		gl.glLoadIdentity(); 					//Reset The Projection Matrix

		//Calculate The Aspect Ratio Of The Window
		
		//GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 1f, 50.0f);
		float ratio = (float) (width) / height;
	//	(45, faspect, 1.0f, 525.0f);
		//GLU.gluPerspective(gl, 45, ratio, 1.0f, 525.0f);
		gl.glFrustumf(-ratio, ratio, -1, 1, 2, 525);	

		gl.glMatrixMode(GL10.GL_MODELVIEW); 	//Select The Modelview Matrix
		gl.glLoadIdentity(); 					//Reset The Modelview Matrix

	}

	/*
	 * (non-Javadoc)
	 * @see android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10, javax.microedition.khronos.egl.EGLConfig)
	 */
	@Override 
	public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
		gl.glShadeModel(GL10.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	 
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glEnable(GL10.GL_NORMALIZE);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		
		float[] rgba = {1f, 1f, 1f, 0f};
		//float lmodel_ambient[]={1f,1f,1f,1.0f};		
		//gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
        gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL10.GL_FRONT, GL10.GL_SHININESS, 15f);       
        gl.glEnable(GL10.GL_COLOR_MATERIAL);
	}
	
	public void setPortViewAngle(float x, float y){
		   for(GLDrawable obj : meshes)
			   obj.setAngles(x, y, 0);
	}
}
